package mk.jug.academy.model;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@MappedSuperclass @SuppressWarnings("serial")
public abstract class User implements BaseEntity, Serializable {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	@Column @Temporal(TemporalType.TIMESTAMP)
	private Date modified;
	
	@Column
	private String imageKey;

	@Column(nullable = false)
	private String email;

	@Column(nullable = false)
	private String password;
	
	@Column
	private String role;	
	
	@Column
	private String salt;

	@Column
	private Integer rating;
	
	public User() {
		this.salt = UUID.randomUUID().toString();
	}
			
	/** Provides the display name of the user. */
	public abstract String getDisplayName();
	
	/**
	 * Returns the id.
	 */
	public Long getId() {
		return id;
	}

	/**
	 * Setter for the id.
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * Returns the modified.
	 */
	public Date getModified() {
		return modified;
	}

	/**
	 * Setter for the modified.
	 */
	public void setModified(Date modified) {
		this.modified = modified;
	}
		
	/**
	 * Returns the imageKey.
	 */
	public String getImageKey() {
		return imageKey;
	}

	/**
	 * Setter for the imageKey.
	 */
	public void setImageKey(String imageKey) {
		this.imageKey = imageKey;
	}

	/**
	 * Returns the email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Setter for the email.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * Returns the password.
	 */
	public String getPassword() {
		return password;
	}
	
	/**
	 * Setter for the password.
	 */
	public void setPassword(String password) {
		this.password = password;
	}		
	
	/**
	 * Returns the role.
	 */
	public String getRole() {
		return role;
	}

	/**
	 * Setter for the role.
	 */
	public void setRole(String role) {
		this.role = role;
	}	
	
	/**
	 * Returns the salt.
	 */
	public String getSalt() {
		return salt;
	}

	/**
	 * Setter for the salt.
	 */
	public void setSalt(String salt) {
		this.salt = salt;
	}

	/**
	 * Returns the rating.
	 */
	public Integer getRating() {
		if (rating == null) {
			return 0;
		}
		return rating;
	}

	/**
	 * Setter for the rating.
	 */
	public void setRating(Integer rating) {
		this.rating = rating;
	}	

	public void incrementRating() {
		if (rating == null) {
			rating = 0;
		}
		rating++;
	}
	
	public void decrementRating() {
		if (rating == null) {
			rating = 0;
		}
		rating--;
	}
	
	  @Override
		public boolean equals(Object obj) {
		  if (obj instanceof BaseEntity) {
			  BaseEntity entity = (BaseEntity) obj;
			  if (this.getId().equals(entity.getId()) && this.getModified().equals(entity.getModified())) {
				  return true;
			  }
		  }
		  return super.equals(obj);
		}

}